package dao.impl;

import Model.User;
import dao.UserDao;
import utils.JdbcUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class UserDaoImpl implements UserDao {

    @Override
    public User getUserByID(int userId) throws SQLException {
        String sql = "select * from t_user where id = ?";
        ResultSet rs = JdbcUtils.executeQuery(sql, userId);
        User user = null;
        while (rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString("username");
            String password = rs.getString("password");
            String gender = rs.getString("gender");
            String phone = rs.getString("phone");
            user = new User();
            user.setId(id);
            user.setUsername(name);
            user.setPassword(password);
            user.setGender(gender);
            user.setPhone(phone);
        }
        return user;
    }
    @Override
    public User getUserByName(String username) throws SQLException {
        String sql = "select * from t_user where username = ?";
        ResultSet rs = JdbcUtils.executeQuery(sql, username);
        User user = null;
        while (rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString("username");
            String password = rs.getString("password");
            String gender = rs.getString("gender");
            String phone = rs.getString("phone");
            user = new User();
            user.setId(id);
            user.setUsername(name);
            user.setPassword(password);
            user.setGender(gender);
            user.setPhone(phone);
        }
        return user;
    }

    @Override
    public ArrayList<User> getAllUser() throws SQLException {
        ArrayList<User> users = new ArrayList<>();
        String sql = "select * from t_user";
        ResultSet rs = JdbcUtils.executeQuery(sql);

        while (rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString("username");
            String password = rs.getString("password");
            String gender = rs.getString("gender");
            String phone = rs.getString("phone");

            User user = new User();
            user.setId(id);
            user.setUsername(name);
            user.setPassword(password);
            user.setGender(gender);
            user.setPhone(phone);
            users.add(user);
        }
        return users;
    }

    @Override
    public int addUser(User user) {
        String sql = "insert into t_user(username, password) values(?, ?)";
        return JdbcUtils.executeUpdate(sql, user.getUsername(), user.getPassword());
    }

    @Override
    public int updateUser(User user) {
        String sql = "update t_user set username = ?, password = ?, phone =?, gender = ? where id = ?;";
        return JdbcUtils.executeUpdate(sql, user.getUsername(), user.getPassword(),user.getPhone(),user.getGender(),user.getId());
    }

    @Override
    public int deleteUser(int id) {
        String sql = "delete from t_user where id = ?";
        return JdbcUtils.executeUpdate(sql, id);
    }
}
